<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}
</style>

<template>
<div class="department">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加库房</a>
    </div>
    <!-- <div class="layui-row">
        <div class="layui-col-lg4 select-item" style="text-align: right">
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" v-model="searchName" placeholder="名称" class="layui-input key">
            </div>
            <button type="button" class="layui-btn sou" @click="search">搜索</button>
        </div>
    </div> -->
    <form class="layui-form">

        <div class="layui-col-lg4 select-item" style="text-align: right;width: auto">
            <div class="layui-input-inline" style="width: 200px">
                <input type="text" v-model="searchName" placeholder="名称" class="layui-input key">
            </div>
            <div class="layui-input-inline" style="width: 200px">
                <select class="layui-select" lay-verify="required" id="enable" lay-filter="shrtype" style="width: 200px">
                    <option value="">请选择状态</option>
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 200px">
                <select class="layui-select" lay-verify="required" id="shrtype" lay-filter="shrtype" style="width: 200px" lay-search>
                    <option value="">请选择所属种植户</option>
                </select>
            </div>
            <!--<div class="layui-input-inline" style="width: 200px">
                <select class="layui-select" lay-verify="required" id="type" lay-filter="type" style="width: 200px">
                    <option value="">请选择库房类型</option>
                </select>
            </div>-->
             <div class="layui-input-inline">
                <button type="button" style="width: 100px" class="layui-btn sou" @click="search">搜索</button>
            </div>

        </div>

    </form>
    <table class="layui-table layui-form">

        <thead>
            <tr>
                <th>库房编号</th>
                <th>库房名称</th>
                <!-- <th>管理人员</th> -->
                <th>联系人</th>
                <th>联系电话</th>
                <!-- <th>库存容量(吨)</th> -->
                <!-- <th>标准</th> -->
                <th>省市区</th>
                <th>详细地址</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in departments">
                <td>{{val.code}}</td>
                <td>{{val.name}}</td>
                <!-- <td>{{val.managername}}</td> -->
                <td>{{val.contact}}</td>
                <td>{{val.contacttel}}</td>
                <!-- <td>{{val.capacity}}</td> -->
                <!-- <td>{{val.standardName}}</td> -->
                <td>{{val.province}} {{val.city}} {{val.area}}</td>
                <td>{{val.address}}</td>
                <td v-if="1 == val.enable">启用</td>
                <td v-if="0 == val.enable">禁用</td>
                <td><a href="javascript:;" @click="edit(val.guid)">修改</a> | <a href="javascript:;" @click="del(val.guid,val.code)">删除</a>
                    | <a href="javascript:;" @click="enableFunc(val.guid,val.enable)" v-if="val.enable == 0">启用</a>
                    | <a href="javascript:;" @click="enableFunc(val.guid,val.enable)" v-if="val.enable == 1">禁用</a>
                </td>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
export default {
    name: 'StoreroomF',
    components: {
        Pagination
    },
    data() {
        return {
            departments: [],
            currentPage: 1,
            totalPage: 0,
            searchName: '',
            enable: null,
            shrtype: [],
            type: []
        }
    },
    mounted() {
        let that = this;
        layui.use(['form', "table"], function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
            that.loadEmnu();
        });

    },
    updated() {
        layui.form.render("select");
        layui.form.render();
    },
    methods: {
        loadEmnu: function () {
            let postParam = {};
            postParam['entity.userType'] = 2;
            $.ajax({
                url: '/system/Tuser!getUserAll.action',
                type: "get",
                dataType: "json",
                data: postParam,
                success: function (res) {
                    this.shrtype = res.info;
                    $.each(res.info, function (index, item) {
                        console.log(item)
                        $('#shrtype').append("<option   value=" + item.guid + " > " + item.realname + "</option>"); // 下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }
            });
        },
        /*loadEmnu126: function () {
            var that = this;
            $.ajax({
                url: '/sales/Sellform!getDictEmnu.action',
                type: "post",
                dataType: "json",
                data: {
                    dicttype: "126"
                },
                success: function (res) {
                    $.each(res.info, function (index, item) {
                        $('#type').append("<option   value=" + item.dictkey + " > " + item.dicttext + "</option>"); // 下拉菜单里添加元素
                        //重新渲染
                        layui.form.render("select");
                    });

                }
            });
        },*/
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = {};
            if (type != 'search') {
                postData.page = that.currentPage;
            } else {
                postData.page = 1;
            }
            postData.name = that.searchName;
            postData.enable = $("#enable").val()
            postData.farmerid = $("#shrtype").val()
            postData.type = 2

            $.ajax({
                url: '/repository/Storeroom!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    console.log(res);

                    if (res != undefined && res.rows != undefined) {
                        //分页数据赋值
                        // that.currentPage = res.indexPage;
                        that.totalPage = res.pageCount;
                        that.departments = res.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddStoreroomF');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditStoreroomF');
        },
        del: function (id, likeCode) {
            let that = this;

            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                $.ajax({
                    url: '/repository/Storeroom!del.action',
                    type: "post",
                    data: {
                        guid: id
                    },
                    dataType: "json",
                    success: function (res) {
                        var result = res.info;
                        if (result != "fail") {
                            that.toast("删除成功");
                            that.getPageData();
                        } else {
                            console.log(result)
                            that.toast(res.errorMsg);
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            })
        },
        enableFunc: function (id, enable) {
            let that = this;
            var title = "";
            if (1 == enable) {
                title = "确定要禁用该库房？";
            }
            if (0 == enable) {
                title = "确定要启用该库房？";
            }

            layer.confirm(title, function () {
                let loader = layer.load();
                $.ajax({
                    url: '/repository/Storeroom!enable.action',
                    type: "post",
                    data: {
                        guid: id,
                        enable: enable
                    },
                    dataType: "json",
                    success: function (res) {
                        var result = res.info;
                        if (result != "fail") {
                            that.toast("操作成功");
                            that.getPageData('search');
                        } else {
                            console.log(result)
                            that.toast(res.errorMsg);
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            })
        },
        delcheck: function (guid) {
            var result1 = "ok";
            $.ajax({
                url: '/repository/Storehouseroom!list.action',
                type: "post",
                data: {
                    storeguid: guid
                },
                dataType: "json",
                success: function (res) {
                    if (res.rows.length != 0) {
                        result1 = "此库房下有仓库，不能删除！";
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
            return result1;
        }

    }
}
</script>
